//

#include <cmath>

#include "basicmenu.hpp"
#include "global.hpp"

void BasicMenu::Release()
{
	cursor.Release();
	font.Release();
	menuItem.clear();
}

void BasicMenu::Reset()
{
	this->cursorIndex = 0;
	rotation = 0;
}

void BasicMenu::Update( double frameTime )
{
	rotation += 360 * frameTime;
	if( rotation > 360 )
		rotation = fmod( rotation, 360.0f );
}

void BasicMenu::Down()
{
	cursorIndex++;
	if( cursorIndex >= menuItem.size() )
	{
		cursorIndex = 0;
	}
}

void BasicMenu::Up()
{
	cursorIndex--;
	if( cursorIndex < 0 )
	{
		cursorIndex = menuItem.size() - 1;
	}
}

void BasicMenu::Render()
{
	float saveX=0, saveY=0;

	int posY = top;

	for( int i = 0; i < menuItem.size(); ++i )
	{
		font.RenderTextCentered( menuItem[i], posY, Global::Game::Width );
		//font.RenderText( menuItem[i], left, posY );

		if( i == cursorIndex )
		{
			saveY = posY + font.Height()/2;
		}

		posY += font.Height() + font.Height()*0.5 ;
	}

	float horizontal = font.CalcStringWidth( menuItem[ cursorIndex ] )/2 + cursor.Width()/2;

	glPushMatrix();
	glTranslatef(
		Global::Game::Width/2 - horizontal - 8,
		saveY, 0 );
	glRotatef( rotation, 0, 0, 1 );
	cursor.RenderOrigin();
	//cursor.Render(
	//	Global::Game::Width/2 - font.CalcStringWidth( menuItem[ cursorIndex ] )/2 - cursor.Width() - 8,
	//	saveY );

	glPopMatrix();

	glPushMatrix();
	glTranslatef(
		Global::Game::Width/2 + horizontal + 8,
		saveY, 0 );
	glRotatef( rotation, 0, 0, -1 );
	cursor.RenderOrigin();
	//cursor.Render(
	//	Global::Game::Width/2 - font.CalcStringWidth( menuItem[ cursorIndex ] )/2 - cursor.Width() - 8,
	//	saveY );

	glPopMatrix();

}
